﻿using Microsoft.Extensions.Caching.Memory;
using System;
using Caching = Microsoft.Extensions.Caching.Memory;

namespace ZingFrameworkCore.Common.Cache.Memory
{
    public class MemoryCache
    {
        public static IMemoryCache _memoryCache = new Caching.MemoryCache(new MemoryCacheOptions());

        public static void Add<T>(string key, T value, int minute, bool isAbsolute = true)
        {
            if (value == null) return;
            if (isAbsolute == true)
            {
                _memoryCache.Set<T>(key, value, new MemoryCacheEntryOptions()
                        .SetAbsoluteExpiration(TimeSpan.FromMinutes(minute)));
                return;
            }
            _memoryCache.Set<T>(key, value, new MemoryCacheEntryOptions()
                        .SetSlidingExpiration(TimeSpan.FromMinutes(minute)));
        }

        public static void Add<T>(string key, T value)
        {
            _memoryCache.Set<T>(key, value);
        }

        public static void Remove(string key)
        {
            _memoryCache.Remove(key);
        }

        public static T Get<T>(string key)
        {
            return _memoryCache.Get<T>(key);
        }

    }
}
